<?php
	defined( "AZ_EXE_" ) or die ( "ERROR : CORE VARIABLE IS NOT DEFINED." );
	class Controller extends AZ {
		private $viewVars = array();
		private $view = NULL ;
		private $language = "" ;
		private $layout = array();
		
		public function __construct( ){
			$this->loadLanguage();
			$this->layout = array();
			$this->viewVars = array();
			$this->view = new View( $this->layout , $this->viewVars , $this->language );
		}
		
		private $css = "" ;
		private $js = "" ;
		
		public function setCss($cssName=""){
			$this->css=$cssName;
		}
		public function setJs($jsName=""){
			$this->js=$jsName;
		}
		
		
		private function loadLanguage(){
			isset( $_REQUEST["lang"] ) && $_REQUEST["lang"] != "" ? $_SESSION["lang"]=$_REQUEST["lang"]:( isset($_SESSION["lang"])&&$_SESSION["lang"]!=""?true:$_SESSION["lang"]="kh");
			$this->language = $_SESSION["lang"];
		}
		public function setLanguage( $language ){
			$this->language = $language;
		}
		public function renderLayout( $layoutName ){
			$this->view = new View( array( "Layout" => $layoutName ) , $this->viewVars , $this->language );
			$this->view->setCss($this->css);
			$this->view->setJs($this->js);
			$this->view->render();
		}
		
		public function renderElement( $elementName ){
			$this->view = new View( array( "Element" => $elementName ) , $this->viewVars , $this->language );
			$this->view->setCss($this->css);
			$this->view->setJs($this->js);
			$this->view->render();
		}
		
		public function renderDefault( $layoutName , $controllerName ){
			$this->view = new View( array( "Default" => $layoutName ) , $this->viewVars , $this->language );
			$this->view->setCss($this->css);
			$this->view->setJs($this->js);
			$this->view->render( $controllerName );
		}
		
		public function set($one, $two = null) {
			$data = NULL ;
			if (is_array($one)) {
				if (is_array($two)) {
					$data = array_combine($one, $two);
				} else {
					$data = $one;
				}
			} else {
				$data = array($one => $two);
			}
			$this->viewVars = $data + $this->viewVars ;
		}
		
		public function getLang($langKeyName){
			return $this->view->getLang($langKeyName);
		}
		
	}
?>